//
//  XKTradeBuyAndSellCell.m
//  XKJY
//
//  Created by xuyaguang on 2017/1/13.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "XKTradeBuyAndSellCell.h"
#import "XKNewsHeaderSelectView.h"
#import "XKMineMarketController.h"

static NSString *XKTradeMarketCellID = @"XKTradeMarketCellReuseIdentifier";

@interface XKTradeBuyAndSellCell () <XKNewsSelectViewDelegate, XKMineMarketControllerDelegate, UITextFieldDelegate>

@property (nonatomic, strong) XKNewsHeaderSelectView *headerView;       // 买卖/选择视图

@property (nonatomic, strong) UITextField   *tradeTextFieldOne;      // 买入/卖出 价格
@property (nonatomic, strong) UITextField   *tradeTextFieldTwo;      // 买入/卖出 数量
@property (nonatomic, strong) UITextField   *tradePwdTextField; // 交易密码
@property (nonatomic, strong) UILabel       *factorageLabel;    // 手续费
@property (nonatomic, strong) UILabel       *turnoverLabel;     // 交易额

@property (nonatomic, strong) UIButton      *plusBtnOne;        // 价格 加号
@property (nonatomic, strong) UIButton      *plusBtnTwo;        // 数量 加号
@property (nonatomic, strong) UIButton      *minusBtnOne;       // 价格 减号
@property (nonatomic, strong) UIButton      *minusBtnTwo;       // 数量减号


@property (nonatomic, strong) XKMineMarketController *tableViewVC;      // 大盘信息
@property (nonatomic, strong) UIButton               *tradeBtn;         // 底部交易按钮(买入/卖出)

@end
@implementation XKTradeBuyAndSellCell

#pragma mark - 自定义cell重写构造方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self configArguments];
        [self createSubviews];
    }
    return self;
}


#pragma mark - 设置自身
- (void)configArguments {
    
    self.tradeType = TRADETYPE_BUY;
    self.backgroundColor = WhiteColor;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}


#pragma mark - 代理方法传回顶买入/卖出按钮点击
- (void)btnClickWithBtn:(UIButton *)clickBtn {
    self.tradeBtn.tag = 20000000 + clickBtn.tag;
    self.tradeBtn.enabled = NO;
    [self.tradeBtn setTitle:clickBtn.titleLabel.text forState:UIControlStateNormal];
    self.tradeTextFieldOne.text = @"";
    self.tradeTextFieldTwo.text = @"";
    self.tradePwdTextField.text = @"";
    self.tradeTextFieldOne.placeholder = [NSString stringWithFormat:@"%@价格",clickBtn.titleLabel.text];
    self.tradeTextFieldTwo.placeholder = [NSString stringWithFormat:@"%@数量",clickBtn.titleLabel.text];
    if (clickBtn.tag == 1) {
        self.tradeType = TRADETYPE_BUY;
    } else if(clickBtn.tag == 2) {
        self.tradeType = TRADETYPE_SELL;
    }
    
    [self calculateResults];
}

#pragma mark - cell 点击自动传入数据
- (void)didSelectedCell:(NSString *)type price:(NSString *)price number:(NSString *)number {
    
    if ([type isEqualToString:@"sell"]) {
        [self.headerView leftBtnClick];
    } else if ([type isEqualToString:@"buy"]) {
        [self.headerView rightBtnClick];
    }
    
    self.tradeTextFieldOne.text = price;
    self.tradeTextFieldTwo.text = number;
    
    [self calculateResults];
}


#pragma mark - 买卖交易
- (void)tradeBtnClick:(UIButton *)sender {
    
    TRADETYPE type = sender.tag - 20000001;
    NSString *price = self.tradeTextFieldOne.text;
    NSString *number = self.tradeTextFieldTwo.text;
    NSString *password = self.tradePwdTextField.text;
    if ([self.delegate respondsToSelector:@selector(tradeWithType:price:number:password:)]) {
        [self.delegate tradeWithType:type price:price number:number password:password];
    }
}


- (void)clearData {
    self.tradeTextFieldOne.text = @"";
    self.tradeTextFieldTwo.text = @"";
    self.tradePwdTextField.text = @"";
    
    // 手续费百分比
    [self calculateResults];
    self.tradeBtn.enabled = NO;
}


#pragma mark - 加减点击
- (void)addOrSubtractBtnCalck:(UIButton *)sender {
    
    if (sender.tag == 0) {
        self.tradeTextFieldOne.text = [NSString stringWithFormat:@"%.2f",(self.tradeTextFieldOne.text.doubleValue + 0.01)];
    } else if (sender.tag == 1) {
        self.tradeTextFieldTwo.text = [NSString stringWithFormat:@"%zd",(self.tradeTextFieldTwo.text.integerValue + 1)];
    } else if (sender.tag == 2 && self.tradeTextFieldOne.text.doubleValue > 0) {
        self.tradeTextFieldOne.text = [NSString stringWithFormat:@"%.2f",(self.tradeTextFieldOne.text.doubleValue - 0.01)];
    } else if (sender.tag == 3 && self.tradeTextFieldTwo.text.integerValue > 0) {
        self.tradeTextFieldTwo.text = [NSString stringWithFormat:@"%zd",(self.tradeTextFieldTwo.text.integerValue - 1)];
    }
    
    [self calculateResults];
}


#pragma mark - 计算
- (void)calculateResults {
    
    // 手续费百分比
    NSString *trade_fee = [UserDefaults objectWithKey:@"trade_fee"];
    if ([trade_fee isEqualToString:@""]) {
        trade_fee = @"0.0";
    }
    
    // 实际费率
    NSString *rate = [NSString calculateNumberOne:trade_fee calculateType:CalculateType_Dividing numberTwo:@"100"];
    
    NSString *numOne = self.tradeTextFieldOne.text;
    NSString *numTwo = self.tradeTextFieldTwo.text;
    
    // 实际交易额(没有四舍五入)
    NSString *result = [NSString calculateNumberOne:numOne calculateType:CalculateType_Multiply numberTwo:numTwo];
    // 计算交易额(四舍五入之后)
    NSString *result_r = [NSString changeNumberFormat:[NSString calculateNumberOne:numOne calculateType:CalculateType_Multiply numberTwo:numTwo withMode:NSRoundPlain scale:2]];
    //实际手续费(没有四舍五入)
    NSString *fee = [NSString changeNumberFormat:[NSString calculateNumberOne:result calculateType:CalculateType_Multiply numberTwo:rate]];
    
    
    
    if (!self.tradeType) {
        self.factorageLabel.text = @"手续费: 0.00 (0.0% CNY)";
        
        if ([result_r isEqualToString:@"0"]) {
            result_r = @"0.00";
        }
        self.turnoverLabel.text = [NSString stringWithFormat:@"交易额: %@ (CNY)",result_r];
    } else {
        self.factorageLabel.text = [NSString stringWithFormat:@"手续费: %@ (%@%% CNY)",fee,trade_fee];
        self.turnoverLabel.text = [NSString stringWithFormat:@"交易额: %@ (CNY)",result_r];
    }
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSMutableString *text = [NSMutableString stringWithString:textField.text];
    [text replaceCharactersInRange:range withString:string];
    
    if (textField == _tradeTextFieldOne) {
        return [text isTradePrice];
    } else if (textField == _tradeTextFieldTwo) {
        return [string isTradeNumber] && text.length <= 10;
    } else if (textField == _tradePwdTextField) {
        return [text isOnlyNumberWithCount:6];
    }
    return YES;
}

#pragma mark - 创建控件
- (void)createSubviews {
    
    _headerView = [XKNewsHeaderSelectView new];
    [_headerView.leftBtn setTitle:@"买入" forState:UIControlStateNormal];
    [_headerView.rightBtn setTitle:@"卖出" forState:UIControlStateNormal];
    self.headerView.delegate = self;
    
    
    _tradeTextFieldOne = [UITextField new];
    _tradeTextFieldOne.keyboardType = UIKeyboardTypeDecimalPad;
    _tradeTextFieldOne.placeholder = @"买入价格";
    _tradeTextFieldOne.font = Font(14);
    _tradeTextFieldOne.textAlignment = NSTextAlignmentCenter;
    _tradeTextFieldOne.borderStyle = UITextBorderStyleRoundedRect;
    _tradeTextFieldOne.backgroundColor = kBaseGrayColor;
    self.tradeTextFieldOne.delegate = self;
    [self.tradeTextFieldOne addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    
    _tradeTextFieldTwo = [[UITextField alloc] init];
    _tradeTextFieldTwo.keyboardType = UIKeyboardTypeNumberPad;
    _tradeTextFieldTwo.placeholder = @"买入数量";
    _tradeTextFieldTwo.font = Font(14);
    _tradeTextFieldTwo.textAlignment = NSTextAlignmentCenter;
    _tradeTextFieldTwo.borderStyle = UITextBorderStyleRoundedRect;
    _tradeTextFieldTwo.backgroundColor = kBaseGrayColor;
    _tradeTextFieldTwo.delegate = self;
    [_tradeTextFieldTwo addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    _tradePwdTextField = [UITextField new];
    _tradePwdTextField.keyboardType = UIKeyboardTypeNumberPad;
    _tradePwdTextField.placeholder = @"交易密码";
    _tradePwdTextField.font = Font(14);
    _tradePwdTextField.secureTextEntry = YES;
    _tradePwdTextField.textAlignment = NSTextAlignmentCenter;
    _tradePwdTextField.borderStyle = UITextBorderStyleRoundedRect;
    _tradePwdTextField.backgroundColor = kBaseGrayColor;
    self.tradePwdTextField.delegate = self;
    [self.tradePwdTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    _factorageLabel = [UILabel getLabWithText:@"手续费: 0.00 (0.0% CNY)" textFont:Font_12 textColor:BlackColor];
    _turnoverLabel = [UILabel getLabWithText:@"交易额: 0.00 (CNY)" textFont:Font_12 textColor:BlackColor];
    
    _plusBtnOne = [UIButton buttonWithType:UIButtonTypeSystem];
    _plusBtnOne.tag = 0;
    _plusBtnOne.frame = CGRectMake(0, 0, 30*Scale_W, 40*Scale_H);
    _plusBtnOne.titleLabel.font = Font(21);
    _plusBtnOne.isIgnore = YES;
    [_plusBtnOne setTitle:@"+" forState:UIControlStateNormal];
    [_plusBtnOne setTitleColor:BlackColor forState:UIControlStateNormal];
    [self.plusBtnOne addTarget:self action:@selector(addOrSubtractBtnCalck:) forControlEvents:UIControlEventTouchUpInside];
    self.tradeTextFieldOne.rightView = _plusBtnOne;
    self.tradeTextFieldOne.rightViewMode = UITextFieldViewModeAlways;
    
    _minusBtnOne = [UIButton buttonWithType:UIButtonTypeSystem];
    _minusBtnOne.tag = 2;
    _minusBtnOne.frame = CGRectMake(0, 0, 30*Scale_W, 40*Scale_H);
    _minusBtnOne.titleLabel.font = Font(21);
    _minusBtnOne.isIgnore = YES;
    [_minusBtnOne setTitle:@"-" forState:UIControlStateNormal];
    [_minusBtnOne setTitleColor:BlackColor forState:UIControlStateNormal];
    [self.minusBtnOne addTarget:self action:@selector(addOrSubtractBtnCalck:) forControlEvents:UIControlEventTouchUpInside];
    self.tradeTextFieldOne.leftView = _minusBtnOne;
    self.tradeTextFieldOne.leftViewMode = UITextFieldViewModeAlways;
    
    _plusBtnTwo = [UIButton buttonWithType:UIButtonTypeSystem];
    _plusBtnTwo.tag = 1;
    _plusBtnTwo.frame = CGRectMake(0, 0, 30*Scale_W, 40*Scale_H);
    _plusBtnTwo.titleLabel.font = Font(21);
    _plusBtnTwo.isIgnore = YES;
    [_plusBtnTwo setTitle:@"+" forState:UIControlStateNormal];
    [_plusBtnTwo setTitleColor:BlackColor forState:UIControlStateNormal];
    [self.plusBtnTwo addTarget:self action:@selector(addOrSubtractBtnCalck:) forControlEvents:UIControlEventTouchUpInside];
    self.tradeTextFieldTwo.rightView = _plusBtnTwo;
    self.tradeTextFieldTwo.rightViewMode = UITextFieldViewModeAlways;
    
    _minusBtnTwo = [UIButton buttonWithType:UIButtonTypeSystem];
    _minusBtnTwo.tag = 3;
    _minusBtnTwo.frame = CGRectMake(0, 0, 30*Scale_W, 40*Scale_H);
    _minusBtnTwo.titleLabel.font = Font(21);
    _minusBtnTwo.isIgnore = YES;
    [_minusBtnTwo setTitle:@"-" forState:UIControlStateNormal];
    [_minusBtnTwo setTitleColor:BlackColor forState:UIControlStateNormal];
    [self.minusBtnTwo addTarget:self action:@selector(addOrSubtractBtnCalck:) forControlEvents:UIControlEventTouchUpInside];
    self.tradeTextFieldTwo.leftView = _minusBtnTwo;
    self.tradeTextFieldTwo.leftViewMode = UITextFieldViewModeAlways;
    

    _tableViewVC = [XKMineMarketController new];
    _tableViewVC.delegate = self;
    
    _tradeBtn = [UIButton getClickBtnWithTitle:@"买入"];
    _tradeBtn.tag = 20000001;
    [self.tradeBtn addTarget:self action:@selector(tradeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView addSubview:self.headerView];
    [self.contentView addSubview:self.tradeTextFieldOne];
    [self.contentView addSubview:self.tradeTextFieldTwo];
    [self.contentView addSubview:self.tradePwdTextField];
    [self.contentView addSubview:self.factorageLabel];
    [self.contentView addSubview:self.turnoverLabel];
    [self.contentView addSubview:self.tableViewVC.view];
    [self.contentView addSubview:self.tradeBtn];
    
    
    [self addConstraints];
}





#pragma mark - 添加约束
- (void)addConstraints {

    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self.contentView);
        make.height.mas_equalTo(45*Scale_H);
    }];
    
    [self.tradeTextFieldOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerView.mas_bottom).offset(10*Scale_H);
        make.left.mas_equalTo(self.contentView).offset(10*Scale_W);
        make.right.mas_equalTo(self.contentView.mas_centerX).offset(-5*Scale_W);
        make.height.mas_equalTo(40*Scale_H);
    }];
    
    [self.tradeTextFieldTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.tradeTextFieldOne.mas_bottom).offset(10*Scale_H);
        make.left.right.height.mas_equalTo(self.tradeTextFieldOne);
    }];
    
    [self.tradePwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.tradeTextFieldTwo.mas_bottom).offset(10*Scale_H);
        make.left.right.height.mas_equalTo(self.tradeTextFieldOne);
    }];
    
    [self.factorageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.tradeTextFieldOne);
        make.top.mas_equalTo(self.tradePwdTextField.mas_bottom).offset(15*Scale_H);
    }];
    
    [self.turnoverLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.tradeTextFieldOne);
        make.top.mas_equalTo(self.factorageLabel.mas_bottom).offset(15*Scale_H);
    }];

    
    [self.tableViewVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerView.mas_bottom).offset(8*Scale_H);
        make.right.mas_equalTo(self.contentView).offset(-0*Scale_W);
        make.width.mas_equalTo(Main_Width/2-0*Scale_W);
        make.height.mas_equalTo(220*Scale_H);
        make.bottom.mas_equalTo(self.contentView).offset(-60*Scale_H).priorityHigh();
    }];
    
    
    [self.tradeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.tableViewVC.view.mas_bottom).offset(20*Scale_H);
        make.left.mas_equalTo(self.contentView).offset(10*Scale_W);
        make.right.mas_equalTo(self.contentView).offset(-10*Scale_W);
        make.height.mas_equalTo(35*Scale_H);
    }];
}

#pragma mark - textField内容改变时
- (void)textFieldDidChange:(UITextField *)textField {
    
    if (self.tradeTextFieldOne.text.length > 0 &&
        self.tradeTextFieldTwo.text.length > 0 &&
        self.tradePwdTextField.text.length == 6) {
        self.tradeBtn.enabled = YES;
        if ([self.tradeBtn.titleLabel.text isEqualToString:@"买入"]) {
            [self.tradeBtn setBackgroundImage:[UIImage imageWithColor:kBaseRedColor] forState:UIControlStateNormal];
        } else if ([self.tradeBtn.titleLabel.text isEqualToString:@"卖出"]) {
            [self.tradeBtn setBackgroundImage:[UIImage imageWithColor:UIColorFromRGB(0x4eb34f)] forState:UIControlStateNormal];
        }
    } else {
        self.tradeBtn.enabled = NO;
    }
    
    [self calculateResults];
    
}




@end
